*****************************************************************************
* .do-file to adjust the individual file to then combine with family files
*****************************************************************************

clear all
set maxvar 7000

**** note: individuals can be uniquely identified by wave, famid68 and persid ****

///////////////////////////////////////////////////////////
//////////////////////// wave 2017 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER34501 famid		// 2017 interview number	
ren ER30002 persid		// 1968 person number 
ren ER30001 famid68		// 1968 family identifier
ren ER34502 sequnum		// 2017 sequence number

ren ER34650 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER34504 ageind		// age of individual
ren ER32000 sexind		// sex of indivdiual
ren ER34506 birthyear   // year of birth individual
ren ER34548 educind		// completed years of education (coded as actual number), indivdiual
ren ER34503 rel_ref     // relation to reference person
ren ER34507 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2017adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file2017indfam.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 2015 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER34301 famid		// 2015 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER34302 sequnum		// 2015 sequence number
ren ER34413 weightind   // core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER34305 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER34307 birthyear	// year of birth individual
ren ER34349 educind		// completed years of education (coded as actual number), indivdiual
ren ER34303 rel_ref		// relation to reference person
ren ER34308 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2015adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file2015indfam.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 2013 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER34201 famid		// 2013 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER34202 sequnum		// 2013 sequence number
ren ER34268 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER34204 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER34206 birthyear	// year of birth individual
ren ER34230 educind		// completed years of education (coded as actual number), indivdiual
ren ER34203 rel_ref		// relation to reference person
ren ER34207 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2013adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file2013indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2011 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER34101 famid		// 2011 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER34102 sequnum		// 2011 sequence number
ren ER34154 weightind 	// weights only avaialbe for immigrant & core sample combined; longitudinal

// demographics (referring to individual)
ren ER34104 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER34106  birthyear	// year of birth individual
ren ER34119 educind		// completed years of education (coded as actual number), indivdiual
ren ER34103 rel_ref		// relation to reference person
ren ER34107 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))


// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2011adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file2011indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2009 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER34001 famid		// 2009 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER34002 sequnum		// 2009 sequence number
ren ER34045 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER34004 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER34006 birthyear	// year of birth individual
ren ER34020 educind		// completed years of education (coded as actual number), indivdiual
ren ER34003 rel_ref		// relation to reference person
ren ER34007 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2009adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file2009indfam.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 2007 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33901 famid		// 2007 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33902 sequnum		// 2007 sequence number
ren ER33950 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER33904 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER33906 birthyear	// year of birth individual
ren ER33917 educind		// completed years of education (coded as actual number), indivdiual
ren ER33903 rel_ref		// relation to reference person
ren ER33907 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2007adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file2007indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2005 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33801 famid		// 2005 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33802 sequnum		// 2005 sequence number
ren ER33848 weightind	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER33804 ageind		// age of individual
ren ER32000 sexind		// sex of indivdiual
ren ER33806 birthyear	// year of birth individual
ren ER33817 educind		// completed years of education (coded as actual number), indivdiual
ren ER33803 rel_ref		// relation to reference person
ren ER33807 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2005adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file2005indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 2003 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33701 famid		// 2003 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33702 sequnum		// 2003 sequence number
ren ER33740 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER33704 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER33706 birthyear	// year of birth individual
ren ER33716 educind		// completed years of education (coded as actual number), indivdiual
ren ER33703 rel_ref		// relation to reference person
ren ER33707 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2003adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file2003indfam.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 2001 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33601 famid		// 2001 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33602 sequnum		// 2001 sequence number
ren ER33637 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER33604 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER33606 birthyear	// year of birth individual
ren ER33616 educind		// completed years of education (coded as actual number), indivdiual
ren ER33603 rel_ref		// relation to reference person
ren ER33607 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file2001adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*

save  "$psidwkddata/file2001indfam.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 1999 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33501 famid		// 1999 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33502 sequnum		// 1999 sequence number
ren ER33546 weightind 	// core indivdiual weight (longitudinal)

ren ER33504 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER33506 birthyear	// year of birth individual
ren ER33516 educind		// completed years of education (coded as actual number), indivdiual
ren ER33503 rel_ref		// relation to reference person
ren ER33507 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1999adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file1999indfam.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 1997 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33401 famid		// 1997 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33402 sequnum		// 1997 sequence number
ren ER33430 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER33404 ageind		// age of individual
ren ER32000 sexind		// sex of indivdiual
ren ER33406 birthyear	// year of birth individual
ren ER33415 educind		// completed years of education (coded as actual number), indivdiual
ren ER33403 rel_ref		// relation to reference person
ren ER33407 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1997adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file1997indfam.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 1996 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33301 famid 		// 1996 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33302 sequnum		// 1996 sequence number
ren ER33318 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER33304 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER33306 birthyear	// year of birth individual
ren ER33315 educind		// completed years of education (coded as actual number), indivdiual
ren ER33303 rel_ref		// relation to reference person
ren ER33307 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1996adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file1996indfam.dta", replace

///////////////////////////////////////////////////////////
//////////////////////// wave 1995 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33201 famid		// 1995 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33202 sequnum		// 1995 sequence number
ren ER33275 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER33204 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER33206 birthyear	// year of birth individual
ren ER33215 educind		// completed years of education (coded as actual number), indivdiual
ren ER33203 rel_ref		// relation to reference person
ren ER33207 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))


// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1995adj.dta"
keep if _merge == 3 // all from using were matched!
drop _merge

drop ER*
save "$psidwkddata/file1995indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1994 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER33101 famid		// 1994 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER33102 sequnum		// 1994 sequence number
ren ER33119 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER33104 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER33106 birthyear	// year of birth individual
ren ER33115 educind		// completed years of education (coded as actual number), indivdiual
ren ER33103 rel_ref		// relation to reference person
ren ER33107 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1994adj.dta"
keep if _merge == 3 // all from using were matched!
drop _merge

drop ER*
save "$psidwkddata/file1994indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1993 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER30806 famid		// 1993 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER30807 sequnum		// 1993 sequence number
ren ER30864 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER30809 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER30811 birthyear	// year of birth individual
ren ER30820 educind		// completed years of education (coded as actual number), indivdiual
ren ER30808 rel_ref		// relation to reference person
ren ER30812 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1993adj.dta"
keep if _merge == 3 // all from using were matched!
drop _merge

drop ER*
save "$psidwkddata/file1993indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1992 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER30733 famid		// 1992 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER30734 sequnum		// 1992 sequence number
ren ER30803 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER30736 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER30738 birthyear	// year of birth individual
ren ER30748 educind		// completed years of education (coded as actual number), indivdiual
ren ER30735 rel_ref		// relation to reference person
ren ER30739 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))


// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1992adj.dta"
keep if _merge == 3 // all from using were matched!!
drop _merge

drop ER*
save "$psidwkddata/file1992indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1991 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER30689 famid		// 1991 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER30690 sequnum		// 1991 sequence number
ren ER30730 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER30692 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER30694 birthyear	// year of birth individual
ren ER30703 educind		// completed years of education (coded as actual number), indivdiual
ren ER30691 rel_ref		// relation to reference person
ren ER30695 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1991adj.dta"
keep if _merge == 3 // all from using were matched!
drop _merge

drop ER*
save "$psidwkddata/file1991indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1990 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER30642 famid		// 1990 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER30643 sequnum		// 1990 sequence number
ren ER30686 weightind 	// longitudinal

// demographics (referring to individual)
ren ER30645 ageind		// age of individual
ren ER32000 sexind		// sex of indivdiual
ren ER30647 birthyear	// year of birth individual
ren ER30657 educind		// completed years of education (coded as actual number), indivdiual
ren ER30644 rel_ref		// relation to reference person
ren ER30648 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1990adj.dta"
keep if _merge == 3 // all from using were matched!
drop _merge

drop ER*
save "$psidwkddata/file1990indfam.dta", replace


///////////////////////////////////////////////////////////
//////////////////////// wave 1989 ////////////////////////
///////////////////////////////////////////////////////////

use "$psidwkddata/IND2017ER.dta", clear

ren ER30606 famid		// 1989 interview number
ren ER30002 persid		// 1968 person number
ren ER30001 famid68		// 1968 family identifier
ren ER30607 sequnum		// 1989 sequence number
ren ER30641 weightind 	// core indivdiual weight (longitudinal)

// demographics (referring to individual)
ren ER30609 ageind		// age of individual
ren ER32000 sexind		// sex of individual
ren ER30611 birthyear	// year of birth individual
ren ER30620 educind		// completed years of education (coded as actual number), indivdiual
ren ER30608  rel_ref	// relation to reference person
ren ER30612 martind		// marital pair indicator (>=1 refers to specific couple, 0: either not married/cohabiting, or spouse not in FU, or not in core sample (which will be dropped anyway))

// merge to family file
merge m:1 famid famid68 using "$psidwkddata/file1989adj.dta"
keep if _merge == 3 // all from using were matched!
drop _merge

drop ER*
save "$psidwkddata/file1989indfam.dta", replace


//////////////////////////////////////////////////////////
///////////////// COMBINE ALL WAVES //////////////////////
//////////////////////////////////////////////////////////

use "$psidwkddata/file2017indfam.dta", clear

foreach num of numlist 2015(-2)1997 {
append using "$psidwkddata/file`num'indfam.dta"
erase "$psidwkddata/file`num'indfam.dta"
}

foreach num of numlist 1996(-1)1989 {
append using "$psidwkddata/file`num'indfam.dta"
erase "$psidwkddata/file`num'indfam.dta"
}

erase  "$psidwkddata/file2017indfam.dta"

save "$psidwkddata/famind_combined.dta", replace
